package com.vy.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vy.stl.entity.Coach;
import com.vy.stl.entity.Course;
import com.vy.stl.entity.Member;
import com.vy.stl.entity.Reserve;
import com.vy.stl.mapper.CourseMapper;
import com.vy.stl.mapper.MemberMapper;
import com.vy.stl.mapper.ReserveMapper;
import com.vy.stl.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.xml.ws.Action;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @create: 2025/09/02 19:19
 * @description:
 */
@Service
public class ReserveServiceImpl implements ReserveService {

    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<Reserve> search(Reserve reserve) {
        return reserveMapper.select(reserve);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Reserve reserve) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 10;
        //开启分页查询
        PageHelper.startPage(page, limit);
        List<Reserve> reserves = reserveMapper.select(reserve);
        PageInfo<Reserve> pageInfo = new PageInfo<>(reserves);
        return pageInfo;
    }

    @Override
    public int add(Reserve reserve) throws Exception {
        //1. 根据课程id和会员id判断是否预约过
        Reserve dbReserve = reserveMapper.selectByMemberIdAndCourseId(reserve);
        if (!ObjectUtils.isEmpty(dbReserve)) {
            throw new Exception("该预约信息已经存在，无法重复预约");
        }
        //2. 核对课程是否存在
        Course course = courseMapper.selectByPrimaryKey(reserve.getCourseId());
        if (ObjectUtils.isEmpty(course)) {
            throw new Exception("该课程已经不存在，无法预约");
        }
        //3. 核对会员是否存在
        Member member = memberMapper.selectByParimaryKey(reserve.getMemberId());
        if (ObjectUtils.isEmpty(member)) {
            throw new Exception("该会员已经不存在，无法预约");
        }
        //4. 只能在开课前一个小时之内预约
        Date nowDate = new Date();
        //当前时间的毫秒值
        long nowDateTime = nowDate.getTime();
        if ((course.getTime().getTime() - nowDateTime) > (60 * 60 * 1000)) {
            throw new Exception("只能在开课前一个小时之内预约");
        }
        //5. 课程已经开课也不能预约
        if (nowDateTime > course.getTime().getTime()  && nowDateTime <= course.getTime().getTime()+course.getLength() * 60 * 1000) {
            throw new Exception("课程已经开课，不能预约");
        }
        //6. 课程已结束也不能预约
        if (course.getTime().getTime() < nowDateTime && nowDateTime > course.getTime().getTime()+course.getLength() * 60 * 1000) {
            throw new Exception("课程已经结束，不能预约");
        }
        reserve.setStatus(1);
        reserve.setScore(5.0);
        reserve.setReserveTime(nowDate);
        return reserveMapper.insert(reserve);
    }

    @Override
    public int edit(Reserve reserve, boolean isJob) throws Exception {
        //1. 根据课程id和会员id判断是否预约过
        Reserve dbReserve = reserveMapper.selectByMemberIdAndCourseId(reserve);
        if (!ObjectUtils.isEmpty(dbReserve) && reserve.getStatus() != 0 && !isJob) {
            throw new Exception("该预约信息已经存在，无法重复预约");
        }
        //2. 核对课程是否存在
        Course course = courseMapper.selectByPrimaryKey(reserve.getCourseId());
        if (ObjectUtils.isEmpty(course)) {
            throw new Exception("该课程已经不存在，无法预约");
        }
        //3. 核对会员是否存在
        Member member = memberMapper.selectByParimaryKey(reserve.getMemberId());
        if (ObjectUtils.isEmpty(member)) {
            throw new Exception("该会员已经不存在，无法预约");
        }
        //4. 只能在修改后的课程开课前一个小时之内预约
        Date nowDate = new Date();
        //当前时间的毫秒值
        long nowDateTime = nowDate.getTime();
        if (!isJob && (course.getTime().getTime() - nowDateTime) > (60 * 60 * 1000)) {
            throw new Exception("只能在开课前一个小时之内预约");
        }
        //5. 课程已经开课也不能预约
        if (!isJob && nowDateTime > course.getTime().getTime()  && nowDateTime <= course.getTime().getTime()+course.getLength() * 60 * 1000) {
            throw new Exception("课程已经开课，不能预约");
        }
        //6. 课程已结束也不能预约
        if (!isJob && course.getTime().getTime() < nowDateTime && nowDateTime > course.getTime().getTime()+course.getLength() * 60 * 1000) {
            throw new Exception("课程已经结束，不能预约");
        }
        return reserveMapper.edit(reserve);
    }

    @Override
    public int editStatus(Reserve reserve) throws Exception {
        Reserve dbReserve = reserveMapper.selectByMemberIdAndCourseId(reserve);
        if (ObjectUtils.isEmpty(dbReserve)) {
            throw new Exception("该预约信息不存在，无法操作");
        }
        return reserveMapper.edit(reserve);
    }

    @Override
    @Transactional
    public int batchUpdateStatus(List<Reserve> reserveList) {
        // 校验参数合法性
        if (ObjectUtils.isEmpty(reserveList)) {
            throw new IllegalArgumentException("预约列表不能为空");
        }

        // 批量更新状态（具体实现根据ORM框架调整）
        // 1. MyBatis方式：调用mapper的批量更新方法
        return reserveMapper.batchUpdateStatus(reserveList);

        // 2. JPA方式：循环更新或使用批量操作
        // reserveRepository.saveAll(reserveList);
    }
}